from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By

import page
from base.base import Base


class PageStudy(Base):

    def pagestudy_click_study_1(self):
        self.base_click_element(page.study_1_xuexi)

    def pagestudy_click_study_cover_close(self):
        self.base_click_element(page.study_1_cover_close)

    def pagestudy_click_letter(self, letter):
        loc = By.XPATH, f"//*[@text ='{letter}']"
        self.base_click_element(loc)

    def pagestudy_get_current_page_letter(self):
        return self.base_get_element_text(page.study_1_letter_roma)

    def pagestudy_click_bottom_btn(self):
        self.base_click_element(page.study_1_bottom_btn)

    def pagestudy_click_play_write_btn(self):
        self.base_click_element(page.study_1_play_write)

    def pagestudy_click_write_btn(self):
        self.base_click_element(page.study_1_write)

    def pagestudy_click_write_clear_btn(self):
        self.base_click_element(page.study_1_clear)

    def pagestudy_write_something(self):
        ta = TouchAction(self.driver)
        el = self.base_find_element(page.study_1_write_area, timeout=3)
        y = el.location.get("y")
        x = el.location.get("x")
        width = el.size.get("width")
        height = el.size.get("height")
        start_x = x
        start_y = y
        end_x = x + width
        end_y = y + height
        ta.press(x=start_x, y=start_y).move_to(x=end_x, y=end_y).release().perform()

    def pagestudy_click_bottom_back_btn(self):
        self.base_click_element(page.study_1_bottom_return)

    def pagestudy_click_study_video(self):
        self.base_click_element(page.study_video)

    def pagestudy_click_study_video_play(self):
        self.base_click_element(page.study_video_play)

    def pagestudy_click_video_full_screen(self):
        while True:
            self.base_click_element(page.study_video_frame)
            try:
                self.base_click_element(page.study_viseo_play_full_screen)
                break
            except:
                pass

    def pagestudy_click_video_play_and_pause(self):
        while True:
            self.base_click_element(page.study_video_frame)
            try:
                self.base_click_element(page.study_video_play_btn)
                break
            except:
                pass


